package com.eastfair.venueservice.config.task;

import com.eastfair.core.context.ContextUtil;
import com.eastfair.projectcore.enumeration.MenuAffiliationEnum;
import com.eastfair.venueservice.dto.ServiceOrderDTO;
import com.eastfair.venueservice.entity.ServiceOrder;
import com.eastfair.venueservice.enumeration.ServiceOrderOrderSourceEnum;
import com.eastfair.venueservice.service.*;
import core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务订单定时任务.
 *
 * @author dqq
 * @date 2023 /4/4
 */
@Slf4j
@Component
public class ServiceOrderTask {

    @Value("${com.eastfair.projectId}")
    private Long projectId;

    @Resource
    private ServiceOrderService serviceOrderService;

    @Resource
    private HomeServiceOrderService homeServiceOrderService;

    @Resource
    private VenueManageServiceOrderService venueManageServiceOrderService;

    @Resource
    private HomeRepairServiceOrderService homeRepairServiceOrderService;

    @Resource
    private VenueRepairServiceOrderService venueRepairServiceOrderService;

    /**
     * 主场搭建商超时未支付取消订单任务执行.
     */
    @XxlJob("cancelTimeoutNotPayHomeServiceOrder")
    @Transactional(rollbackFor = Exception.class)
    public void cancelTimeoutNotPayHomeServiceOrder() {
        log.info("cancelTimeoutNotPayHomeServiceOrder - 主场搭建商超时未支付取消订单任务执行");
        ContextUtil.setProjectId(projectId);
        ServiceOrderDTO query = new ServiceOrderDTO();
        List<String> belongSystemList = new ArrayList<>();
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_PRODUCER.getCode());
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_COURT.getCode());
        query.setBelongSystemList(belongSystemList);
        List<ServiceOrderOrderSourceEnum> orderOrderSourceEnumList = new ArrayList<>();
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.BUILDER_REPORT);
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.BUILDER_ONLINE_BOOK);
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.HOME_BUILDER_ORDER);
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.HOME_EXHIBITOR_ORDER);
        query.setOrderSourceEnumList(orderOrderSourceEnumList);
        List<ServiceOrder> list = serviceOrderService.listTimeoutNotPayServiceOrder(query);
        if (list == null || list.isEmpty()) {
            log.info("cancelTimeoutNotPayServiceOrder - 没有超时需要取消的订单");
            return ;
        }
        list.forEach(serviceOrder -> {
            try {
                homeServiceOrderService.cancelTimeoutNotPayServiceOrder(serviceOrder.getOrderNumber());
            } catch (Exception e) {
                log.error("超时未支付取消订单失败, orderNumber={}", serviceOrder.getOrderNumber(), e);
            }
        });
    }

    /**
     * 综合服务超时未支付取消订单任务执行.
     */
    @XxlJob("cancelTimeoutNotPayVenueManageServiceOrder")
    @Transactional(rollbackFor = Exception.class)
    public void cancelTimeoutNotPayVenueManageServiceOrder() {
        log.info("cancelTimeoutNotPayVenueManageServiceOrder - 综合服务超时未支付取消订单任务执行");
        ContextUtil.setProjectId(projectId);
        // 查询超时未支付的订单
        ServiceOrderDTO query = new ServiceOrderDTO();
        List<String> belongSystemList = new ArrayList<>();
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_VENUE.getCode());
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_TRANSPORT.getCode());
        query.setBelongSystemList(belongSystemList);
        List<ServiceOrderOrderSourceEnum> orderOrderSourceEnumList = new ArrayList<>();
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.ONLINE_BOOK);
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.ON_SITE_ORDER);
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.REPORT_ORDER);
        query.setOrderSourceEnumList(orderOrderSourceEnumList);
        List<ServiceOrder> list = serviceOrderService.listTimeoutNotPayServiceOrder(query);
        if (list == null || list.isEmpty()) {
            log.info("cancelTimeoutNotPayServiceOrder - 没有超时需要取消的订单");
            return;
        }
        list.forEach(serviceOrder -> {
            try {
                venueManageServiceOrderService.cancelTimeoutNotPayServiceOrder(serviceOrder.getOrderNumber());
            } catch (Exception e) {
                log.error("超时未支付取消订单失败, orderNumber={}", serviceOrder.getOrderNumber(), e);
            }
        });
    }

    @XxlJob("cancelTimeoutNotPayVenueRepairServiceOrder")
    @Transactional(rollbackFor = Exception.class)
    public void cancelTimeoutNotPayVenueRepairServiceOrder() {
        log.info("cancelTimeoutNotPayVenueRepairServiceOrder - 场馆报修订单超时未支付取消订单任务执行");
        ContextUtil.setProjectId(projectId);
        // 查询超时未支付的订单
        ServiceOrderDTO query = new ServiceOrderDTO();
        List<String> belongSystemList = new ArrayList<>();
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_VENUE.getCode());
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_TRANSPORT.getCode());
        query.setBelongSystemList(belongSystemList);
        List<ServiceOrderOrderSourceEnum> orderOrderSourceEnumList = new ArrayList<>();
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.VENUE_HOME_REPAIR_ORDER);
        query.setOrderSourceEnumList(orderOrderSourceEnumList);
        List<ServiceOrder> list = serviceOrderService.listTimeoutNotPayServiceOrder(query);
        if (list == null || list.isEmpty()) {
            log.info("cancelTimeoutNotPayServiceOrder - 没有超时需要取消的订单");
            return;
        }
        list.forEach(serviceOrder -> {
            try {
                venueRepairServiceOrderService.cancelTimeoutNotPayServiceOrder(serviceOrder.getOrderNumber());
            } catch (Exception e) {
                log.error("超时未支付取消订单失败, orderNumber={}", serviceOrder.getOrderNumber(), e);
            }
        });
    }

    @XxlJob("cancelTimeoutNotPayHomeRepairServiceOrder")
    @Transactional(rollbackFor = Exception.class)
    public void cancelTimeoutNotPayHomeRepairServiceOrder() {
        log.info("cancelTimeoutNotPayHomeRepairServiceOrder - 主场报修订单超时未支付取消订单任务执行");
        ContextUtil.setProjectId(projectId);
        // 查询超时未支付的订单
        ServiceOrderDTO query = new ServiceOrderDTO();
        List<String> belongSystemList = new ArrayList<>();
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_PRODUCER.getCode());
        belongSystemList.add(MenuAffiliationEnum.P_COMPOSITE_COURT.getCode());
        query.setBelongSystemList(belongSystemList);
        List<ServiceOrderOrderSourceEnum> orderOrderSourceEnumList = new ArrayList<>();
        orderOrderSourceEnumList.add(ServiceOrderOrderSourceEnum.HOME_BUILDER_REPAIR_ORDER);
        query.setOrderSourceEnumList(orderOrderSourceEnumList);
        List<ServiceOrder> list = serviceOrderService.listTimeoutNotPayServiceOrder(query);
        if (list == null || list.isEmpty()) {
            log.info("cancelTimeoutNotPayServiceOrder - 没有超时需要取消的订单");
            return;
        }
        list.forEach(serviceOrder -> {
            try {
                homeRepairServiceOrderService.cancelTimeoutNotPayServiceOrder(serviceOrder.getOrderNumber());
            } catch (Exception e) {
                log.error("超时未支付取消订单失败, orderNumber={}", serviceOrder.getOrderNumber(), e);
            }
        });
    }
}